package com.dulayev.chrono;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class TimeLine extends SQLiteOpenHelper {

	static private final String TASK = "Task";
	static private final String TIME_LINE = "TimeLine";
	static private final String ID = "id";	
	static private final String NAME = "name";	
	static private final String START_AT = "startAt";	
	
	public TimeLine(Context context) {
		super(context, "timeline.db", null, 1);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(String.format("CREATE TABLE %s (%s INTEGER PRIMARY KEY AUTOINCREMENT, %s TEXT)", TASK, ID, NAME));
		db.execSQL(String.format("CREATE TABLE %s (%s INTEGER2, %s INTEGER4)", TIME_LINE, TASK, START_AT));
	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		throw new RuntimeException("Upgrade database is unexpected");
	}

	public void addTask(String taskName) {
		SQLiteDatabase db = getWritableDatabase();
		try {
			Integer taskId = findTask(db, taskName);
			if(taskId == null)
			{
				ContentValues values = new ContentValues();
				values.put(NAME, taskName);
				db.insert(TASK, "", values);
			}
		} finally {
			db.close();
		}
	}

	private Integer findTask(SQLiteDatabase db, String taskName) {
		Cursor cursor = db.query(TASK, new String[]{ ID }, NAME + " = ?", new String[]{ taskName }, null, null, null);
		try {
			if(!cursor.moveToNext())
				return null; 
			return cursor.getInt(0);
		} finally {
			cursor.close();
		}
	}
	
	private String getTaskName(SQLiteDatabase db, int taskId) {
		
		Cursor cursor = db.query(TASK, new String[]{ NAME }, ID + " = ?", new String[]{ String.valueOf(taskId) }, null, null, null);
		try {
			if(!cursor.moveToNext())
				return null; 
			return cursor.getString(0);
		} finally {
			cursor.close();
		}
	}
	
	public String[] getTasks() {
		SQLiteDatabase db = getReadableDatabase();
		try {
			Cursor cursor = db.query(TASK, new String[]{ NAME }, null, null, null, null, null);
			try {
				List<String> list = new ArrayList<String>(); 
				while(cursor.moveToNext())
					list.add(cursor.getString(0));
				return list.toArray(new String[list.size()]);
			} finally {
				cursor.close();
			}
		} finally {
			db.close();
		}
	}
	
	public void startTask(String taskName) {
		SQLiteDatabase db = getWritableDatabase();
		try {
			Integer taskId = findTask(db, taskName);
			if(taskId == null)
				throw new RuntimeException(String.format("No Task with name %s found", taskName));
			
			ContentValues values = new ContentValues();
			values.put(TASK, taskId);
			values.put(START_AT, System.currentTimeMillis() / 1000L); 
			
			db.insert(TIME_LINE, "", values);
		} finally {
			db.close();
		}
	}

	public String getActiveTask() {
		SQLiteDatabase db = getReadableDatabase();
		try {
			Cursor cursor = db.query(TIME_LINE, new String[]{ TASK }, null, null, null, null, START_AT + " DESC");
			try {
				if(!cursor.moveToNext())
					return null;
				int taskId = cursor.getInt(0);
				return getTaskName(db, taskId);
			} finally {
				cursor.close();
			}
		} finally {
			db.close();
		}
	}
}
